package leetcodecompetition.S119;

import org.junit.jupiter.api.Test;
import tool.s;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Siantar
 * @Date: 2023-12-09-22:43
 * @Description: 1.0
 */
public class T100147 {
    public int maxSubarrayLength(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        int left = 0, right = 0, n = nums.length, res = 0;
        while (right < n) {
            int x = map.getOrDefault(nums[right], 0);
            if (x == k) {
                int p = nums[right];
                while (nums[left] != p) {
                    map.put(nums[left], map.get(nums[left]) - 1);
                    left++;
                }
                map.put(nums[left], map.get(nums[left]) - 1);
                left++;
                x--;
            }
            map.put(nums[right], x+1);
            right++;
            res=Math.max(res,right-left);
        }
        return res;
    }

    @Test
    public void test() {
        System.out.println(maxSubarrayLength(new int[]{1,2,3,1,2,3,1,2}, 2));
    }
}
